$(document).ready(function (){
    // onload时获取用户信息，判断是否有效
    $.ajax({
        url:"/user?method=getUserInfo",
        type:"get",
        dataType:"json",
        xhrFields: {
            withCredentials: true
        },
        success: res => {
            const {code, msg} = res;
            // 有效直接跳主页
            if (code === 200) {
                window.location.href = "/pages/index.jsp"
            }
        },
        error: err => {
            console.log(err)
        }
    })

    // 取缓存，存在赋值
    let cacheStr = localStorage.getItem("userCache");
    if (cacheStr !== null && cacheStr !== '' && cacheStr) {
        let userCache = JSON.parse(cacheStr);
        $("#username").val(userCache.username);
        $("#password").val(userCache.password);
    }
    // 记住我事件监听
    $("#rememberMe").click(function (e) {
        let flag = e.target.checked;
        if (flag) {
            let userCache = {
              username: $("#username").val(),
              password: $("#password").val()
            };
            localStorage.setItem("userCache", JSON.stringify(userCache));
        } else {
            localStorage.removeItem("userCache");
        }
    })
})